package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.config.R;
import com.itheima.reggie.dao.CategoryDao;
import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dao.DishFlavorDao;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class DishServiceImpl implements DishService {


    @Autowired(required = false)
    private DishDao dishDao;

    @Autowired(required = false)
    private DishFlavorDao dishFlavorDao;
    @Autowired(required = false)
    private CategoryDao categoryDao;
    @Autowired(required = false)
    private RedisTemplate redisTemplate;


    /**
     * 菜品的添加
     *
     * @param dishDto
     */
    @Transactional
    @Override
    public void save(DishDto dishDto) {
        //1. 补全菜品的数据
        dishDto.setStatus(1); // 1代表菜品启用  0代表下架商品
        dishDto.setSort(0);
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        //2.保存菜品
        dishDao.save(dishDto);

        //3.得到菜品的口味信息
        List<DishFlavor> flavorList = dishDto.getFlavors();
        //4. 给一个口味信息补全
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishDto.getId()); //口味对应的菜品的id
            dishFlavor.setCreateTime(dishDto.getCreateTime());
            dishFlavor.setUpdateTime(dishDto.getUpdateTime());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setIsDeleted(0);
        }

        //5. 批量插入菜品口味
        dishFlavorDao.batchSave(flavorList);
        Set keys = redisTemplate.keys("dish_*"); //获取所有以dish_xxx开头的key
        redisTemplate.delete(keys); //删除这些key
    }

    /**
     * get
     * 查询菜品信息
     */
    @Override
    public Page<DishDto> findAll(Integer page, Integer pagesize, String name) {

        List<Dish> dishList = dishDao.findAllByName(name);
        PageHelper.startPage(page, pagesize);
        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Category cateById = categoryDao.findNameById(dish.getCategoryId());
            dishDto.setCategoryName(cateById.getName());
            dishDtoList.add(dishDto);
        }
        Page<DishDto> dishDtoPage = new Page<>(dishDtoList, pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum());
        return dishDtoPage;
    }

    /*
        s数据回显
     */
    @Override
    public DishDto findCatefory(Long dishId) {
        Dish dishById = dishDao.findAllById(dishId);
        List<DishFlavor> DishFlavor = dishFlavorDao.findAllFlavorByDishId(dishId);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dishById, dishDto);
        dishDto.setFlavors(DishFlavor);
        return dishDto;
    }

    /**
     * 更新数据
     *
     * @param dishDto
     * @return
     */
    @Override
    public void upDateDish(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDao.update(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        dishFlavorDao.deleteDishFlavor(dishDto.getId());
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dishDto.getId()); //口味对应的菜品的id
            dishFlavor.setCreateTime(dishDto.getCreateTime());
            dishFlavor.setUpdateTime(dishDto.getUpdateTime());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setIsDeleted(0);
        }
        dishFlavorDao.batchSave(flavors);
    }

    /**
     * 找菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishDto> findDishById(Long categoryId, Integer status) {
        List<DishDto> dishDtoList = null;
//        优化先去redis里边找，找不到再去数据库找，然后保存到redis
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = "dish_" + categoryId + "_" + status;
        if (valueOperations.get(key)!= null) {
            dishDtoList = (List<DishDto>) valueOperations.get(key);
        } else {
            List<Dish> list = dishDao.findDishById(categoryId, status);
            dishDtoList = new ArrayList<>();
            for (Dish dish : list) {
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(dish, dishDto);
                List<DishFlavor> allFlavorByDishId = dishFlavorDao.findAllFlavorByDishId(dish.getId());
                dishDto.setFlavors(allFlavorByDishId);
                dishDtoList.add(dishDto);
            }
            valueOperations.set(key,dishDtoList,1, TimeUnit.DAYS);
        }
        return dishDtoList;
    }
}
